//
//  PlayerNames.m
//  ScorePad
//
//  Created by Andy on 08/09/2010.
//  Copyright 2010 Mister Smith's Maevellous Mechanical Software Company. All rights reserved.
//

#import "PlayerNamesViewController.h"

@implementation PlayerNamesViewController

@synthesize delegate;

@synthesize playerNames;
@synthesize numberOfPlayers;

@synthesize numberOfPlayersButton;

@synthesize label1;
@synthesize name1;
@synthesize label2;
@synthesize name2;
@synthesize label3;
@synthesize name3;
@synthesize label4;
@synthesize name4;
@synthesize label5;
@synthesize name5;
@synthesize label6;
@synthesize name6;
@synthesize label7;
@synthesize name7;

@synthesize activeField;

static NSInteger playersInt;

- (void)viewDidLoad {
	
	[super viewDidLoad];
	
	name1.text = [playerNames objectAtIndex:0];
	name2.text = [playerNames objectAtIndex:1];
	name3.text = [playerNames objectAtIndex:2];
	name4.text = [playerNames objectAtIndex:3];
	name5.text = [playerNames objectAtIndex:4];
	name6.text = [playerNames objectAtIndex:5];
	name7.text = [playerNames objectAtIndex:6];
	
	keyboardShown = NO; 
	
	self.registerForKeyboardNotifications;	
		
	playersInt = [numberOfPlayers integerValue];
	numberOfPlayersButton.selectedSegmentIndex = playersInt -1;
	
}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (IBAction)numberOfPlayersChanged:(id) sender {

	UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
	
	playersInt = [segmentedControl selectedSegmentIndex] +1;
	
	CGRect viewFramey = [self.view frame];
	viewFramey.size.height = playersInt * 70;
	viewFramey.size.height += 100;		/* Account for 'number of players' segmented control */
	[(UIScrollView*)self.view setContentSize:CGSizeMake(viewFramey.size.width,viewFramey.size.height)];
	
	[self horribleHidingMethod];
	
	if (keyboardShown)
		[name1 becomeFirstResponder];
	
}

- (void)horribleHidingMethod {
	label2.hidden = YES;
	name2.hidden = YES;
	label3.hidden = YES;
	name3.hidden = YES;
	label4.hidden = YES;
	name4.hidden = YES;
	label5.hidden = YES;
	name5.hidden = YES;
	label6.hidden = YES;
	name6.hidden = YES;
	label7.hidden = YES;
	name7.hidden = YES;
	
	if (playersInt > 1) {
		label2.hidden = NO;
		name2.hidden = NO;
		if (playersInt > 2) {
			label3.hidden = NO;
			name3.hidden = NO;
			if (playersInt > 3) {
				label4.hidden = NO;
				name4.hidden = NO;
				if (playersInt > 4) {
					label5.hidden = NO;
					name5.hidden = NO;
					if (playersInt > 5) {
						label6.hidden = NO;
						name6.hidden = NO;
						if (playersInt > 6) {
							label7.hidden = NO;
							name7.hidden = NO;
						}
					}
				}
			}
		}
	}
}

- (void)viewWillDisappear:(BOOL)animated {
	
	if (([activeField.text isEqualToString:@" "]) ||
		([activeField.text length] == 0))
		activeField.text = @"Player";
	
	NSArray *returnedPlayers = [[NSArray alloc]initWithObjects:
								name1.text,
								name2.text,
								name3.text,
								name4.text,
								name5.text,
								name6.text,
								name7.text,
								nil];
	
	NSNumber *playersTotal = [[NSNumber alloc]initWithInteger:playersInt];
	
	NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsPath = [path objectAtIndex:0];
	NSString *namesPath = [documentsPath stringByAppendingPathComponent:@"namesArray.plist"];
	NSString *numberPath = [documentsPath stringByAppendingPathComponent:@"playersNumber.plist"];
	
	NSArray *tempArray = [[NSArray alloc] initWithObjects:playersTotal, nil];
	
	[returnedPlayers writeToFile:namesPath atomically:YES];
	[tempArray writeToFile:numberPath atomically:YES];
	
	
	if(delegate && [delegate respondsToSelector:@selector(playersDataReturned:totalPlayers:)]) 
		[delegate playersDataReturned: returnedPlayers totalPlayers: playersTotal]; 
	
	[tempArray release];
	[playersTotal release];
	[returnedPlayers release];
}


#pragma mark -
#pragma mark Keyboard Stuff

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(keyboardWasShown:)
												 name:UIKeyboardDidShowNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
	if (keyboardShown)
		return;
	
	NSDictionary* info = [aNotification userInfo];
	
	// Get the size of the keyboard.
	NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
	CGSize keyboardSize = [aValue CGRectValue].size;
	
	// Resize the scroll view (which is the root view of the window)
	CGRect viewFrame = [self.view frame];
	
	viewFrame.size.height -= keyboardSize.height;
	self.view.frame = viewFrame;
	
	[self scrollActiveTextFieldIntoView:activeField];
	
	keyboardShown = YES;	
}

- (void)scrollActiveTextFieldIntoView:(UITextField *)textField
{
	CGRect textFieldRect = [textField frame];
	[(UIScrollView*)self.view scrollRectToVisible:textFieldRect animated:YES];
}

#pragma mark -
#pragma mark Deal with Text Fields


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
	activeField = textField;
	
	[self scrollActiveTextFieldIntoView:activeField];	
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
			
	if (([textField.text isEqualToString:@" "]) ||
	   ([textField.text length] == 0))
		textField.text = @"Player";
		
	return YES;	
}

- (void)dealloc {
	[playerNames release];
	[numberOfPlayers release];
	
	[numberOfPlayersButton removeFromSuperview];
		
	[label1 removeFromSuperview];
	[name1 removeFromSuperview];
	[label2 removeFromSuperview];
	[name2 removeFromSuperview];
	[label3 removeFromSuperview];
	[name3 removeFromSuperview];
	[label4 removeFromSuperview];
	[name4 removeFromSuperview];
	[label5 removeFromSuperview];
	[name5 removeFromSuperview];
	[label6 removeFromSuperview];
	[name6 removeFromSuperview];
	[label7 removeFromSuperview];
	[name7 removeFromSuperview];

	[activeField release];
	
	[super dealloc];
}


@end
